package com.juyu.miliao.scheduler.rabbitMQ;

import com.alibaba.fastjson.JSON;
import com.juyu.miliao.scheduler.dao.mysql.PicCheckDao;
import com.juyu.miliao.scheduler.domin.PicCheck;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 鉴黄功能mq处理机制
 */
@Component
public class PicCheckMQCustomer {
    @Resource
    private AmqpTemplate amqpTemplate;
    @Resource
    private PicCheckDao picCheckDao;
    /**发送后台规则*/
    private static final String EXCHANGE_ONE_TO_ONR_ROOM="EXCHANGE_ONE_TO_ONR_ROOM";
    private static final String ROUTING_KEY_TO_PRI_CHECK = "ROUTING_KEY_TO_PRI_CHECK";
    /**接收apimq规则*/
    private static final String EXCHANGE = "exchange_api_scheduler_picCheck_save";
    private static final String KEY = "routingKey_api_scheduler_picCheck_save";
    private static final String QUEUE = "queue_api_scheduler_picCheck_save";

    @RabbitListener(bindings = @QueueBinding(value = @Queue(QUEUE),
            exchange = @Exchange(EXCHANGE),
            key = KEY))
    public void testMQ(String PicCheckJson){
        PicCheck picCheck = JSON.parseObject(PicCheckJson, PicCheck.class);
        int temp = picCheckDao.addPicCheck(picCheck);
        if(temp!=0){
            sendAddPicCheckMQ(picCheck.getId());
        }
    }

    /** 发送鉴黄图片id给后台mq*/
    public void sendAddPicCheckMQ(int id){
        amqpTemplate.convertAndSend(EXCHANGE_ONE_TO_ONR_ROOM,ROUTING_KEY_TO_PRI_CHECK,id);
    }
}
